//
//  YSOrderBusinessFooterView.m
//  Yueshi
//
//  Created by Northwan on 15/8/3.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//

#import "YSOrderBusinessFooterView.h"
#import "YSOrderListItemStatusObj.h"

@interface YSOrderBusinessFooterView()
@property (strong, nonatomic) UILabel *numLabel;
@property (strong, nonatomic) UILabel *priceLabel;
@property (strong, nonatomic) UILabel *freightLabel;
@property (strong, nonatomic) UIButton *leftBtn;
@property (strong, nonatomic) UIButton *rightBtn;
@property (strong, nonatomic) UIView   *spliteView;


@end

@implementation YSOrderBusinessFooterView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self setBackgroundColor:PUBLIC_CARD_BG];
        //运费
        UILabel *freightLabel = [UILabel new];
        [freightLabel setFont:SYS_FONT(13)];
        [freightLabel setTextColor:HEXColor(@"#333333")];
        self.freightLabel = freightLabel;
        [self addSubview:freightLabel];
        
        //价格
        UILabel *priceLabel = [UILabel new];
        [priceLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13]];
        [priceLabel setTextColor:HEXColor(@"#333333")];
        self.priceLabel = priceLabel;
        [self addSubview:priceLabel];
        
        //数量
        UILabel *numLabel = [UILabel new];
        [numLabel setFont:SYS_FONT(15)];
        [numLabel setTextColor:HEXColor(@"#333333")];
        self.numLabel = numLabel;
        [self addSubview:numLabel];
        
        
        //初始化两个按钮
        UIButton *rightBtn = [UIButton new];
        [rightBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
        [rightBtn setCornerRadius:5 borderWidth:1 borderColor:ThemeColor];
        [rightBtn.titleLabel setFont:SYS_FONT(14)];
        [rightBtn addTarget:self action:@selector(rightClick:) forControlEvents:UIControlEventTouchUpInside];
        self.rightBtn = rightBtn;
        [self addSubview:rightBtn];
        
        //
        UIButton *leftBtn = [UIButton new];
        [leftBtn setTitleColor:HEXColor(@"#333333") forState:UIControlStateNormal];
        [leftBtn setCornerRadius:5 borderWidth:1 borderColor:HEXColor(@"#eaeae9")];
        [leftBtn.titleLabel setFont:SYS_FONT(14)];
        self.leftBtn = leftBtn;
        [leftBtn addTarget:self action:@selector(leftClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:leftBtn];
        
        
        self.spliteView = [UIView new];
        [self.spliteView setBackgroundColor:PUBLIC_BG];
        [self addSubview: self.spliteView];
    }
    return self;
}


-(void)setFooterViewFrame:(YSOrderBusinessFooterViewFrame *)footerViewFrame{
    _footerViewFrame = footerViewFrame;
    YSOrder *order =   footerViewFrame.order;
    NSString *freightString = nil;
    if(order.orderType == OrdinaryOrderType){
        freightString = [NSString stringWithFormat:@"(含运费¥%ld)",order.freight];
    }else{
        freightString = @"";
    }
    [self.freightLabel setText:freightString];
    self.freightLabel.frame = footerViewFrame.freightFrame;
    
    [self.priceLabel setText:order.orderPrice];
    self.priceLabel.frame = footerViewFrame.priceFrame;
    
    NSString *numString = [NSString stringWithFormat:@"共%ld件商品   合计:¥",order.prodCount];
    [self.numLabel setText:numString];
    self.numLabel.frame = footerViewFrame.numFrame;
    
    if(order.orderStatus == OrderExperienceVolumeChildNotPayType
       ||order.orderStatus == OrderOrdinaryChildNotPayType
       ||order.orderStatus == OrderOrdinaryChildNotReceiveType
       ||order.orderStatus == OrderOrdinaryChildNotReviewType)
    {
        self.leftBtn.frame = footerViewFrame.leftBtnFrame;
        self.rightBtn.frame = footerViewFrame.rightBtnFrame;
        [self dealBtnWithStatus:order.orderStatus];
    }
    self.spliteView.frame = footerViewFrame.spliteFrame;
    self.frame = footerViewFrame.frame;
}

-(void)leftClick:(UIButton *)sender{
    NSLog(@"leftClick");
    YSOrderListItemStatusObj *obj = [YSOrderListItemStatusObj new];
    [obj setIsLeft:YES];
    [obj setTag:self.tag];
    [obj setStatus:sender.tag];
    [obj setOrderCode:_footerViewFrame.order.orderCode];
    
    if ([self.delegate respondsToSelector:@selector(orderItemBtnClick:)]) {
        [_delegate orderItemBtnClick:obj];
    }
}

-(void)rightClick:(UIButton *)sender{
    NSLog(@"rightClick");
    YSOrderListItemStatusObj *obj = [YSOrderListItemStatusObj new];
    [obj setIsLeft:NO];
    NSLog(@"tag:%ld----status:%ld",self.tag,sender.tag);
    [obj setTag:self.tag];
    [obj setStatus:sender.tag];
    [obj setOrderCode:_footerViewFrame.order.orderCode];
    if ([self.delegate respondsToSelector:@selector(orderItemBtnClick:)]) {
        [_delegate orderItemBtnClick:obj];
    }
}

-(void)dealBtnWithStatus:(NSInteger)status{
    switch (status) {
        case OrderExperienceVolumeChildNotPayType://待支付
        case OrderOrdinaryChildNotPayType://待支付
            //取消和立即支付
            [self.leftBtn setHidden:NO];
            [self.rightBtn setHidden:NO];
            [self.leftBtn setTitle:@"取消" forState:UIControlStateNormal];
            [self.leftBtn setTag:status];
            [self.rightBtn setTitle:@"立即支付" forState:UIControlStateNormal];
            [self.rightBtn setTag:status];
            break;
        case OrderOrdinaryChildNotSendType://待发货
            [self.leftBtn setHidden:YES];
            [self.rightBtn setHidden:YES];
            [self.leftBtn setTag:status];
            [self.rightBtn setTag:status];
            break;
        case OrderOrdinaryChildNotReceiveType://待收货,已发货
            //查看物流和确认收货
            [self.leftBtn setHidden:NO];
            [self.rightBtn setHidden:NO];
            [self.leftBtn setTitle:@"查看物流" forState:UIControlStateNormal];
            [self.leftBtn setTag:status];
            [self.rightBtn setTitle:@"确认收货" forState:UIControlStateNormal];
            [self.rightBtn setTag:status];
            break;
        case OrderOrdinaryChildNotReviewType://待评价
            //评论晒单
            [self.leftBtn setHidden:YES];
            [self.rightBtn setHidden:NO];
            [self.rightBtn setTitle:@"评论晒单" forState:UIControlStateNormal];
            [self.rightBtn setTag:status];
            break;
        case OrderExperienceVolumeChildNotConsumeType://待消费
            [self.leftBtn setHidden:YES];
            [self.rightBtn setHidden:YES];
            [self.leftBtn setTag:status];
            [self.rightBtn setTag:status];
            break;
        case OrderOrdinaryChildRefundType://退款单
            [self.leftBtn setHidden:YES];
            [self.rightBtn setHidden:YES];
            [self.leftBtn setTag:status];
            [self.rightBtn setTag:status];
            break;
        case OrderOrdinaryChildCancleType://取消的订单
            [self.leftBtn setHidden:YES];
            [self.rightBtn setHidden:YES];
            break;
            
        default:
            break;
    }
}

@end
